package com.gopay.common.cps.dao.fund.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.fund.FundFileDao;
import com.gopay.common.domain.fund.FundGopayFileEntity;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BindCardDAOImpl.java
 * @author baiyunpeng
 * @2016年7月4日 上午10:27:00  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("fundFileDao")
public class FundFileDaoImpl extends BaseDAOImpl<FundGopayFileEntity,String> implements FundFileDao {
    private static final Logger logger = Logger.getLogger(FundFileDaoImpl.class);
    @Override
    public FundGopayFileEntity saveFile(FundGopayFileEntity fileEntity) {
        return save(fileEntity);
    }

    /**
     * 0文件扫表用的,用于corn定时0文件入库
     *
     * @param start
     * @param end
     * @param type
     * @return
     */
    @Override
    public List<FundGopayFileEntity> getFileOntime(String start, String end, String[] type,String fileStatus) {
        if ( end != null && !"".equals(end) && type.length > 0) {

            StringBuffer sb = new StringBuffer();
            sb.append("  from  FundGopayFileEntity t where ");
            sb.append(" to_char(t.settleTime, 'YYYYMMdd')=:dateEnd ");
            sb.append("  and t.fileType=:types");
            sb.append(" and t.fileStatus=:fileStatus");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("dateEnd", end.toString());
            map.put("fileStatus",fileStatus);
            map.put("types",type[0].toString());
            List<FundGopayFileEntity> byHql = findByHql(sb.toString(), map);//123
            return byHql;
        }
        return null;
    }

    /**
     * 更新状态的方法: 支持0文件,3文件,27,28更新
     * 需要哪个属性就传哪个属性值,不需要的设置为null
     *
     * @param maps
     * @param key
     * @return
     */
    @Override
    public Integer updateStatusFile(Map<String, Object> maps, String key) {
        StringBuffer sb = null;
        //文件状态  0.失败，1、成功
        String status = (String) maps.get("fileStatus");

        //对账状态  0,未对账，1：对账失败，2：对账成功(3文件)
        String reconciliationStatus = (String) maps.get("reconciliationStatus");

        //对账时间
        Date reconciliationTime = (Date) maps.get("reconciliationTime");

        //结算状态  0，未结算，1：已结算(3文件27文件)
        String settleStatus = (String) maps.get("settleStatus");

        //结算时间
        Date settleTime = (Date) maps.get("settleTime");

        //修改时间
        Date lastUpdTs = (Date) maps.get("lastUpdTs");

        String custId = (String) maps.get("custId");
        Integer totalSum = (Integer) maps.get("totalSum");
        BigDecimal totalAmount = (BigDecimal) maps.get("totalAmount");

        StringBuffer sql2 = new StringBuffer("");
        sql2.append("UPDATE FundGopayFileEntity set ");

        Map<String, Object> map = new HashMap<String, Object>();

        if(maps.size()>0 && key!=null) {
            if (lastUpdTs!=null) {
                sql2.append(" lastUpdTs=:lastUpdTs ");
                map.put("lastUpdTs", lastUpdTs);
            }
            if (status != null) {
                sql2.append(" ,fileStatus=:states ");
                map.put("states", status);
            }
            if (reconciliationStatus!=null) {
                sql2.append(" ,reconciliationStatus=:reconciliationStatus  ");
                map.put("reconciliationStatus", reconciliationStatus);
            }
            if (reconciliationTime!=null) {
                sql2.append(" ,reconciliationTime=:reconciliationTime ");
                map.put("reconciliationTime", reconciliationTime);
            }
            if (settleStatus!=null) {
                sql2.append(" ,settleStatus=:settleStatus ");
                map.put("settleStatus", settleStatus);
            }
            if (settleTime!=null) {
                sql2.append(" ,settleTime=:settleTime");
                map.put("settleTime", settleTime);
            }
            if (totalSum!=null) {
                sql2.append(" ,totalSum=:totalSum");
                map.put("totalSum", totalSum);
            }
            if (totalAmount!=null) {
                sql2.append(" ,totalAmount=:totalAmount");
                map.put("totalAmount", totalAmount);
            }
            if (custId!=null) {
                sql2.append(" ,custId=:custId ");
                map.put("custId",custId);
            }

            sql2.append("  WHERE keyId=:seq ");
            map.put("seq", key);

            return updateByHql(sql2.toString(), map);

        }

        return null;
    }

    @Override
    public List<FundGopayFileEntity> queryFiles(String isNotify) {

        StringBuffer sb = new StringBuffer();
        sb.append(" from FundGopayFileEntity t where t.fileStatus=1 and t.reconciliationStatus = 0 and t.settleStatus = 0 ");
        sb.append(" and t.isNotify in (:isNotify)");

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("isNotify", isNotify);

        List<FundGopayFileEntity> result = findByHql(sb.toString(), map);
        return result;

    }

    @Override
    public int updateIsNotify(Map<String, Object> param) {
        StringBuffer sb = new StringBuffer();
        sb.append("UPDATE FundGopayFileEntity set isNotify=:isNotify, lastUpdTs=sysdate WHERE keyId=:seq");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("isNotify",param.get("isNotify") != null?param.get("isNotify"):"1");
        map.put("seq",param.get("seq") != null?param.get("seq"):"");
        return updateByHql(sb.toString(), map);
    }

    /**
     * 根据差异文件更新文件状态
     * @param maps
     * @return
     */
    @Override
    public Integer updateFileCauseDiffer(Map<String, Object> maps) {

        //文件创建时间
        String fileName = (String) maps.get("fileName");
        //文件类型
        String fileType = (String) maps.get("fileType");

        String reconciliationStatus = (String) maps.get("reconciliationStatus");

        //结算状态
        String settleStatus = (String) maps.get("settleStatus");
        //结算时间
        Date settleTime = (Date) maps.get("settleTime");
        //最后修改时间
        Date lastUpdTs = (Date) maps.get("lastUpdTs");

            StringBuffer sql = new StringBuffer("UPDATE FundGopayFileEntity set reconciliationStatus=:reconciliationStatus, settleStatus=:settleStatus ," +
                    "  lastUpdTs=:lastUpdTs WHERE fileName like :fileName");

            Map<String, Object> map = new HashMap<>();

            map.put("settleStatus", settleStatus);
            map.put("lastUpdTs", lastUpdTs);
           /* map.put("fileType", fileType);*/
            map.put("fileName",fileName);
            map.put("reconciliationStatus",reconciliationStatus);
            int i = updateByHql(sql.toString(), map);

            return i;


    }


    @Override
    public List<FundGopayFileEntity>queryFileByParam(Map<String, Object> paramMap) {
        StringBuffer hql = new StringBuffer();
        hql.append(" from FundGopayFileEntity where 1=1");
        Map<String, Object> param = new HashMap<String, Object>();

        if (paramMap != null) {
            if (paramMap.get("custId") != null) {
                hql.append(" and custId=:custId");
                param.put("custId", paramMap.get("custId"));
            }

            if (paramMap.get("fileType") != null) {
                hql.append(" and fileType = :fileType ");
                param.put("fileType", paramMap.get("fileType"));
            }
            if (paramMap.get("fileName") != null) {
                hql.append(" and fileName like :fileName ");
                param.put("fileName", paramMap.get("fileName"));
            }

        }
        logger.info(hql.toString()+"参数："+ ToStringBuilder.reflectionToString(param.get("fileName")));
        return this.findByHql(hql.toString(),param);
    }

   @Override
   public List<FundGopayFileEntity> getFileOntime(String fileName, String[] type, String fileStatus, String fundNo){
       if ( fileName != null && !"".equals(fileName) && type.length > 0 && fundNo!=null && !"".equals(fundNo)) {

           StringBuffer sb = new StringBuffer();
           sb.append("  from  FundGopayFileEntity t where ");
           sb.append(" t.fileName like :fileName ");
           sb.append("  and t.fileType=:types ");
           sb.append(" and t.fileStatus=:fileStatus ");
           sb.append(" and t.custId =:cusId ");
           Map<String, Object> map = new HashMap<String, Object>();
           map.put("fileName", fileName.toString());
           map.put("fileStatus", fileStatus);
           map.put("types", type[0].toString());
           map.put("cusId", fundNo);

           List<FundGopayFileEntity> byHql = findByHql(sb.toString(), map);
           return byHql;
       }
       return null;
    }

    @Override
    public FundGopayFileEntity getRedeemFileByNameAndType(String fileName, String type){
        StringBuffer hql = new StringBuffer();
        hql.append(" from FundGopayFileEntity where 1=1");
        Map<String, Object> param = new HashMap<String, Object>();

        hql.append(" and fileStatus = :fileType ");
        param.put("fileType","2");
        hql.append(" and fileName=:fileName ");
        param.put("fileName",fileName);

        List<FundGopayFileEntity> byHql = findByHql(hql.toString(), param);
        if(byHql!=null && byHql.size()>0){
            return byHql.get(0);

        }
        return null;
    }

    @Override
    public FundGopayFileEntity getEntityByKey(String keyId) {
        StringBuffer hql = new StringBuffer();
        hql.append(" from FundGopayFileEntity where keyId=:keyId");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("keyId",keyId);
        List<FundGopayFileEntity> byHql = findByHql(hql.toString(), map);
        if(byHql!=null && byHql.size()>0){
            return byHql.get(0);

        }
        return null;
    }

    @Override
    public List<FundGopayFileEntity> queryFiles(String[] isNotifys) {
        StringBuffer sb = new StringBuffer();
        sb.append(" from FundGopayFileEntity t where t.fileStatus=1 and t.reconciliationStatus = 0 and t.settleStatus = 0 ");
        sb.append(" and t.isNotify in (:isNotify)");

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("isNotify", isNotifys);
        List<FundGopayFileEntity> result = findByHql(sb.toString(), map);
        return result;

    }


}
